<?php
/**
 * Created by PhpStorm.
 * User: Zhoujinda
 * Date: 2016/6/29
 * Time: 14:48
 */
namespace Mengwu\Controller;

use Admin\Api\DatatreeApi;
use Admin\Api\ViewPageApi;
use Admin\Api\ViewPageItemApi;
use Shop\Api\ProductApi;
use Shop\Api\ProductGroupApi;

class ThemeActivitiesController extends BaseStoreController{
    function index(){
        $PICTURE_REMOTE_URL = C('PICTURE_REMOTE_URL');
        $block_id = I('block',-1);
        $map = [
            'name' => '主题活动',
            'notes' => 'PC首页主题活动布局'
        ];
        $result = apiCall(ViewPageApi::GET_INFO,[$map]);
        $page_id = 0;
        if($result['status']){
            if(is_null($result['info'])){
                $this->errorPage('主题活动不存在');
            }else{
                $page_id = $result['info']['id'];
            }
        }else{
            $this->errorPage($result['info']);
        }

        $map = [
            'id' => $block_id,
            'page_id' => $page_id
        ];

        $result = apiCall(ViewPageItemApi::GET_INFO,[$map]);

        if($result['status']){
            if(is_null($result['info'])){
                $this->errorPage('错误的主题活动');
            }
        }

        $item = $result['info'];
        $item_data = json_decode($item['data'],true);
        $banner = $item_data['banners'][0];
        $zhutihuodong = [
            'block_id' => $item_data['block_id'],
            'banner' => $PICTURE_REMOTE_URL.$banner['id'],
            'discount' => $banner['discount'],
            'main_time' => $banner['group_main_time']
        ];
        $this->assign('zhutihuodong',$zhutihuodong);
        $this->assignTitle('主题活动');

        //取商品列表
        $group_product_list = $banner['group_product_list'];
        if(is_null($group_product_list))exit('error group_product_list');

        //获取group_id
        //查找页面商品分组 data id;
        $size = 16;
        $group_id = 0;
        $map = [
            'name' => '页面商品分组',
            'level' => 0
        ];
        $result = apiCall(DatatreeApi::GET_INFO,[$map]);
        if($result['status']){
            if(is_null($result['info'])){
                exit('no group_dt');
            }
            $dt_id = $result['info']['id'];
        }else{
            exit('error 3');
        }
        $group_name = "页面商品分组.$page_id.$block_id.0";
        $map = [
            'name' => $group_name,
            'parentid' => $dt_id,
            'level' => 1
        ];
        $result = apiCall(DatatreeApi::GET_INFO,[$map]);
        if($result['status']){
            if(!is_null($result['info'])){
                $group_id = $result['info']['id'];
                $this->assign('group_id',$group_id);
            }else{
                exit('no group');
            }
        }else{
            exit('error 4');
        }

        $order = I('order',10);
        if(!in_array($order,[10,11,12]))$order = 10;
        if($order=='10'){
            //默认
            $order = false;
        }elseif($order==11){
            //人气
            $order = 'view_cnt desc';

        }elseif($order==12){
            //销量
            $order = "total_sales desc";
        }

        $p_ids = $group_product_list['p_ids'];
        $cate = I('cate',0,'int');
        $map = [];
        if($cate!=0) $map['cate_id'] = $cate;

        $prop_id = I('prop',0);
        $value_id = I('propvalue',0);

        $result = $this->getPropByPid($p_ids,$cate!=0?$cate:false);
        $this->assign('cate_props',$result['info']);

        $cate_list = $this->getCate($p_ids);
        $this->assign('cate_list',$cate_list);

        if(!empty($prop_id) && !empty($value_id)){
            $p_ids = $this->getPidByProp($prop_id,$value_id,$p_ids);
        }

        $result = $this->_queryProduct($p_ids,I('p',1),$size,$group_id,$order,$map);
        if($result['status']){
            $page_no = I('p',1);
            $page_size = 16;
            $show = paging($page_no,$page_size,$result['info']['count'],'mengwu/themeActivities/index');
            $this->assign('show',$show);
            $this->assign('product_count',$result['info']['count']);
            $this->assign('product_list',$result['info']['list']);
        }


        $this->boye_display();
    }

    private function _queryProduct($pids=[],$curpage=1,$size=10,$group_id=false,$order=false,$_map=[]){

        $ids = $pids;
        if(empty($ids))  return ['status'=>true,'info'=>['count'=>0,'list'=>[]]];
        $map = [
            'store_id' => 1,
            'id' => ['in',$ids],
            'onshelf' => 1,
            'status' => 1
        ];

        $map = array_merge($_map,$map);

        $field = ['product_id','name','img','price','dt_origin_country'];
        $page = ['curpage'=>$curpage,'size'=>$size];
        $result = apiCall(ProductApi::QUERY_WITH_COUNT,[$map,$page,$order,false,$field]);
        if($result['status']){
            $list = $result['info']['list'];
            $count = $result['info']['count'];
            $PICTURE_REMOTE_URL = C('PICTURE_REMOTE_URL');

            //获取国别
            $map = [
                'parentid' => 35
            ];
            $result = apiCall(DatatreeApi::QUERY_NO_PAGING,[$map]);
            $countryHash = [];
            if($result['status']){
                foreach ($result['info'] as $val){
                    $countryHash[$val['code']] = $val['name'];
                }
            }
            foreach($list as &$val){
                $val['img'] = $PICTURE_REMOTE_URL.$val['img'].'&size=240';
                $val['country'] = isset($countryHash[$val['dt_origin_country']])?$countryHash[$val['dt_origin_country']]:'';
                if($group_id){
                    //获取group 最低价
                    $map = ['g_id'=>$group_id,'p_id'=>$val['product_id']];
                    $field = "MIN(price) as min_price";
                    $result = apiCall(ProductGroupApi::QUERY_NO_PAGING,[$map,false,$field]);
                    $min_price = $result['info'][0]['min_price'];
                    if($result['status'] && !is_null($min_price)){
                        $val['group_price'] = $min_price;
                    }else{
                        $val['group_price'] = $val['price'];
                    }
                }else{

                }

            }
            if(is_null($list)){
                $list = [];
            }
            return ['status'=>true,'info'=>['count'=>$count,'list'=>$list]];
        }else{
            return ['status'=>false,'info'=>'error'];
        }

    }

    //获取指定产品所属类目
    private function getCate($pid=[]){
        $result = apiCall(ProductApi::QUERY_PRODUCT_CATE_BY_PID,[$pid]);
        if($result['status']){
            return $result['info'];
        }else{
            return [];
        }
    }

    //查询商品属性根据商品组
    private function getPropByPid($pid=[],$cate=false){
        $result = apiCall(ProductApi::QUERY_PROP_BY_PID,[$pid,$cate]);
        if($result['status']){
            return $result['info'];
        }else{
            return [];
        }
    }
    
    //获取指定商品属性商品ID组
    private function getPidByProp($prop_id,$value_id,$pid=[]){
        $map = [];
        if(!empty($pid)){
            $map['pid'] = ['in',$pid];
        }

        $result = apiCall(ProductApi::QUERY_PRODUCT_BY_PROP,[$prop_id,$value_id,$map]);
        if($result['status']){
            return $result['info'];
        }else{
            return [];
        }
    }

}